package com.maxmlr.music.common.param;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;

/**
 * @Package com.maxmlr.music.common.resut
 * @ClassName SimpleReqDTO
 * @Description 公共请求参数
 * @author max
 * @since 2018年12月16日 下午2:03:29
 * @version 1.0
 * @Copyright Copyright (c) 2018: www.maxmlr.com.
 */
public class SimpleReqDTO implements Serializable {
	/** serialVersionUID */
	private static final long serialVersionUID = 1L;

	// 事务ID,确保每次唯一
	private String requestId;

	// 第几页
	private Integer pageNum;

	// 每页显示多少行
	private Integer pageSize;

	// 查询的参数
	private Map<String, Object> params;

	// 扩展字段程序内部使用
	@JSONField(serialize = false)
	private Map<String, Object> extContext = new HashMap<String, Object>();

	public Integer getPageNum() {
		return pageNum;
	}

	public void setPageNum(Integer pageNum) {
		this.pageNum = pageNum;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	public String getRequestId() {
		return requestId;
	}

	public void setRequestId(String requestId) {
		this.requestId = requestId;
	}

	public Map<String, Object> getParams() {
		if (this.params == null) {
			this.params = new HashMap<String, Object>();
		}

		return params;
	}

	public void setParams(Map<String, Object> params) {
		this.params = params;
	}

	@JSONField(serialize = false)
	public String getReqParameter(String key) {
		return parseData(key, String.class);
	}

	@SuppressWarnings("unchecked")
	@JSONField(serialize = false)
	public <T> T parseData(String key, Class<T> clazz) {
		if (params == null) {
			return null;
		}

		Object obj = params.get(key);
		if (obj == null) {
			return null;
		}

		if (obj instanceof JSONObject) {
			return JSON.toJavaObject((JSONObject) obj, clazz);
		}

		return (T) obj;
	}

	public void addParams(String key, Object value) {
		if (key != null) {
			if (this.params == null) {
				this.params = new HashMap<String, Object>();
			}
			this.params.put(key, value);
		}
	}

	public void addParamsMap(Map<String, Object> map) {
		if (map != null) {
			if (this.params == null) {
				this.params = map;
			} else {
				this.params.putAll(map);
			}

		}
	}

	/**
	 * 业务处理查询分页的辅助方法，查询的起始行
	 * 
	 * @return
	 */
	public int getStartRow() {
		if (null == pageNum || null == pageSize) {
			return 0;
		}
		return pageNum > 0 ? (pageNum - 1) * pageSize + 1 : 0;

	}

	/**
	 * 业务处理查询分页的辅助方法，查询的结束行
	 * 
	 * @return
	 */
	public int getEndRow() {
		if (null == pageNum || null == pageSize) {
			return 0;
		}
		return pageNum * pageSize;
	}

	/**
	 * extContext.
	 *
	 * @return the extContext
	 * @since JDK 1.6
	 */
	public Map<String, Object> getExtContext() {
		return extContext;
	}

	public <T> T parseDTO(Class<T> clazz) {
		String jsonStr = JSON.toJSONString(this.getParams());
		if (jsonStr != null && jsonStr.trim().length() > 0) {
			return JSON.parseObject(jsonStr, clazz);
		}

		return null;
	}

	/**
	 * extContext.
	 *
	 * @param extContext
	 *            the extContext to set
	 * @since JDK 1.6
	 */
	public void setExtContext(Map<String, Object> extContext) {
		this.extContext = extContext;
	}

}
